/**
 * 附件上传
 */
(function () {
    function AttachmentObject(element, options) {
        var self = this;
        this.id = "";
        this.options = options;
        this.key = this.options.key ? this.options.key : "key";
        this.element = element;
        this.url = options.url ? options.url : "pic";
        this.picImage = options.picImage ? options.picImage : "pic-image";
        this.fileContainer = options.queueID ? options.queueID : "fileContainer";
        this.path = G_Path.contextPath;
        this.options.onUploadSuccess = function (file, data, response) {
            if (data != null) {
                var json = $.parseJSON(data);
                var url = json.url;
                if (url != null && url != "") {
                    $("#" + self.picImage).attr("src", staticServer + url);
                } else {
                    $("#" + self.picImage).attr("src", "/static/img/default.jpg");
                }
                $("#" + self.url).val(url);
                if (self.callback) {
                    self.callback(file, json);
                }
            }
        };

        this.options.onSelect = function () {
            var notLast = $(self.element).find('div.uploadify-queue-item').not(':last');
            notLast.remove();
            $('#btnContainer').show();
        };
        this.options.onCancel = function () {
            $('#btnContainer').hide();
        };

        this.callback = options.callback ? options.call : null;
        this.init();
        this.show();

    }

    AttachmentObject.DEFAULTS = {
        //'scriptData': {
        //    'id': this.id
        //},
        swf: G_Path.path + '/static/js/plugins/upload/uploadify.swf',
        uploader: G_Path.path + '/upload;jsessionid=', //后台处理的请求
        buttonText: document.getElementById('pic-image').alt,//上传按钮的文字
        'fileSizeLimit': '10MB',
        'buttonClass': "upload-btn",//按钮的样式
        queueSizeLimit: 1, //   default 999
        'method': 'post',//和后台交互的方式：post/get
        queueID: 'fileContainer',
        fileObjName: 'file', //后台接受参数名称
        fileTypeExts: '*.gif;*.png;*.jpg;*.jpeg;', //控制可上传文件的扩展名，启用本项时需同时声明fileDesc
        'auto': true, //true当选择文件后就直接上传了，为false需要点击上传按钮才上传
        'multi': false, //是否支持多个附近同时上传
        height: 44,//选择文件按钮的高度
        width: 100,//选择文件按钮的宽度
        'debug': false,//debug模式开/关，打开后会显示debug时的信息
        'dataType': 'json',
        "url": 'pic',
        "picImage": "pic-image",
        removeCompleted: false//上传成功后的文件，是否在队列中自动删除
        /* onUploadSuccess:function(file, data, response){


         },
         onSelect:function () {
         //插件本身没有单文件上传之后replace的效果
         var notLast = $('#'+this).find('div.uploadify-queue-item').not(':last');
         notLast.remove();
         $('#btnContainer').show();
         },
         onCancel:function () {
         $('#btnContainer').hide();
         }*/
    };
    AttachmentObject.prototype = {
        init: function () {
            var self = this;

            $(self.element).uploadify(self.options);
        },
        show: function () {
            var self = this;
            var scriptData = this.options.scriptData;
            var id = scriptData.id;
        }


    };
    $.fn.att = function (options) {
        var scriptData = options.scriptData;
        this.key = options.key;
        var id = scriptData.id;
        options.url=options.url ? options.url : "pic";
        options.uploader = G_Path.path + '/upload?id=' + id + '&key=' + this.key;
        ';jsessionid=';
        var element = $(this);

        var config = $.extend({}, AttachmentObject.DEFAULTS, options);
        var att = new AttachmentObject(element, config);
    }
})(jQuery);